<?php
App::import('Vendor', 'fckeditor');

class FckHelper extends AppHelper { 
               
    /**
    * creates an fckeditor textarea
    * 
    * @param array $namepair - used to build textarea name for views, array('Model', 'fieldname')
    * @param string $content
    */
    function fckeditor($namepair, $content, $width="100%", $height="100%"){
        $editor_name = 'data';
        foreach ($namepair as $name){
            $editor_name .= "[" . $name . "]";
        }
    
        $oFCKeditor = new FCKeditor($editor_name) ;
        $oFCKeditor->BasePath = $this->webroot.'js/fckeditor/' ;
        $oFCKeditor->Value = $content ;
        $oFCKeditor->Width = $width ;
        $oFCKeditor->Height = $height ;
        $oFCKeditor->Create() ;            
    }      
    
    function fckeditorjs($namepair, $content="", $width="100%", $height="100%"){
        $editor_id = "";
        
        $content=preg_replace("/\n/","",$content);
		$content=preg_replace("/\r/","",$content);
		$content=preg_replace("/'/","\\\'",$content);
		
        if(is_array($namepair)) {
	        foreach ($namepair as $name){
	            $editor_id .= ucfirst($name);
	        }	
        }else {
        	$editor_id = Inflector::camelize(str_replace('.', '_', $namepair)); 
        }
        
		return "<script type=\"text/javascript\">
					fckLoader_".$editor_id." = function () {
					    var bFCKeditor_$editor_id = new FCKeditor('$editor_id');
					    bFCKeditor_".$editor_id.".BasePath = '".$this->webroot."js/fckeditor/';
					    bFCKeditor_".$editor_id.".Value = '$content';
					    bFCKeditor_".$editor_id.".Config['ToolbarStartExpanded'] = false ;
					    bFCKeditor_".$editor_id.".ReplaceTextarea();
					}
					fckLoader_$editor_id();
				</script>";
        
    } 
} 	
	
?>